package cn.wolfcode.wolf2w.business.service;

import cn.wolfcode.wolf2w.business.config.DeepSeekConfig;
import cn.wolfcode.wolf2w.business.config.RestTemplateConfig;
import cn.wolfcode.wolf2w.business.domain.Strategy;
import cn.wolfcode.wolf2w.business.dto.OpenAiChatRequest;
import cn.wolfcode.wolf2w.business.dto.OpenAiChatResponse;
import cn.wolfcode.wolf2w.business.dto.OpenAiMessage;
import com.alibaba.fastjson2.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;

/**
 * @author : 邓风来
 * @date : 2025/9/30 13:40
 */

@Service
public class DeepSeekService {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private DeepSeekConfig deepSeekConfig;
    /**
     *
     * @param question 自定义提示词
     * @param strategies ES中查出来的数据
     * @return deepseek的应答结果
     */
    public String askWithStrategies(String question, List<Strategy> strategies){
        String systemPrompt = buildSystemPrompt(strategies);
        OpenAiMessage systemMessage = new OpenAiMessage("system", systemPrompt);
        OpenAiMessage userMessage = new OpenAiMessage("user", question);
        ArrayList<OpenAiMessage> messages = new ArrayList<>();
        messages.add(systemMessage);
        messages.add(userMessage);

        OpenAiChatRequest requestBody = new OpenAiChatRequest();
        requestBody.setMessages(messages);
        requestBody.setModel(deepSeekConfig.getModel());
        requestBody.setTemperature(0.3);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setBearerAuth(deepSeekConfig.getApiKey());

        HttpEntity<OpenAiChatRequest> httpEntity = new HttpEntity<>(requestBody, headers);

        String url = deepSeekConfig.getBaseurl() + "/chat/completions";

        OpenAiChatResponse response = restTemplate.postForObject(url, httpEntity, OpenAiChatResponse.class);
        if (response == null || response.getChoices() == null || response.getChoices().size() == 0){
            return "抱歉,我无法回答你的问题";
        }
        OpenAiChatResponse.Choice choice = response.getChoices().get(0);
        if (choice == null || choice.getMessage() == null){
            return "抱歉,我无法回答你的问题";
        }
        return choice.getMessage().getContent();
    }

    private String buildSystemPrompt(List<Strategy> strategies) {
        StringJoiner stringJoiner = new StringJoiner("\n\n");
        stringJoiner.add("你是一名知名导游,请基于以下的数据库私有片段进行回答,不要编造数据.若资料不足请明确说明.\n\n" +
                "以下是来在私有知识库的片段(JSON):");
        for (Strategy strategy : strategies) {
            String jsonString = JSON.toJSONString(strategy);
            stringJoiner.add(jsonString);
        }
        return stringJoiner.toString();
    }
}
